package ru.urfc.portal.ui.client.googlets.admin;

import ru.urfc.portal.ui.client.App;
import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Style;
import ru.urfc.portal.ui.client.controllers.ItemsController;
import ru.urfc.portal.ui.client.controllers.events.PostEvent;
import ru.urfc.portal.ui.client.controllers.events.PutEvent;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.descriptor.GridField;
import ru.urfc.portal.ui.client.model.catalogs.booleans.ClientAccessEnabled;
import ru.urfc.portal.ui.client.model.catalogs.booleans.ClientAdminRegistered;
import ru.urfc.portal.ui.client.renderers.BaseRenderer;
import ru.urfc.portal.ui.client.widgets.MegaGrid;

import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;

public class ClientEdit extends BaseGooglet {

	interface Binder extends UiBinder<Widget, ClientEdit> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField Style style;
	@UiField Element adminRegistered;
	@UiField Element accessEnabled;
	@UiField Element changeAccessText;
	
	@UiField Element associationKeyFormTemplate;
	@UiField Element associationKey;

	public Type<ItemsController> CHANGE_ACCESS = new Type<ItemsController>();

	@UiHandler("changeAccess")
	void changeAccess(ClickEvent e) {
		getEventsDispatcher().fireEvent(new PutEvent(CHANGE_ACCESS));
	}
	
	static public class ChangeAccessController extends ItemsController {

		private GridField accessEnabledField;

		public ChangeAccessController(GridDescriptor descriptor, GridField accessEnabledField) {
			super(descriptor);
			this.accessEnabledField = accessEnabledField;
		}
		
		@Override
		public void doPut() {
			if (getModelData() != null) {
				Boolean value = accessEnabledField.getValue(getModelData());
				accessEnabledField.setValue(getModelData(), !value);
				super.doPut();
			}
		}
		
	}
	
	public static final Type<ItemsController> GENERATE_KEY = new Type<ItemsController>();
	
	@UiHandler("generateKey")
	void generateKey(ClickEvent e) {
		getEventsDispatcher().fireEvent(new PostEvent(GENERATE_KEY));
	}

	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		changeAccessText.setInnerText("Запретить доступ в систему");
		
		GridDescriptor descriptor = new GridDescriptor(CLIENT, CLIENTS, getItemId());
		descriptor.addBoolCIField("accessEnabled", ClientAccessEnabled.class, accessEnabled, style);
		descriptor.addBoolCIField("adminRegistered", ClientAdminRegistered.class, adminRegistered, style);
		GridField accessEnabledField = descriptor.addBoolField("accessEnabled", changeAccessText, 
				"Запретить доступ в систему", "Разрешить доступ в систему").setEditable();
		
		ChangeAccessController controller = new ChangeAccessController(descriptor, accessEnabledField);
		register(App.DO_UPDATE, controller);
		register(CHANGE_ACCESS, controller);
		
		initAssociationKey();
	}

	private void initAssociationKey() {
		GridDescriptor descriptor = new GridDescriptor(ASSOCIATION_KEY,
				CLIENTS, getItemId(), CLIENT_GENERATE_ASSOCIATION_KEY);
		GridField keyField = descriptor.addField("key");
		descriptor.assign(new AssociationKeyRenderer(keyField));
		register(GENERATE_KEY, new ItemsController(descriptor));
	}
	
	public class AssociationKeyRenderer extends BaseRenderer {
		
		public AssociationKeyRenderer(GridField keyField) {
			super(keyField);
		}

		@Override
		public void renderElements(ModelData modelData) {
			associationKey.setInnerText(field.<String>getValue(modelData));
			createWindowForPrinting(associationKeyFormTemplate.getInnerHTML());
		}
		
		@Override
		public Object renderCell(ModelData modelData) {
			return null;
		}
		
		@Override
		protected void init(MegaGrid grid) {
		}
		
		@Override
		public void updateModel(ModelData modelData) {
		}
		
	}

}